﻿// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

await GetText();

async Task GetText()
{
    var url = "http://localhost:5023/WeatherForecast/SSEtest";
    var client = new HttpClient();
    using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, url);
    var response = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
    await using var stream = await response.Content.ReadAsStreamAsync();
    var streamReader = new StreamReader(stream);
    var buffer = new Memory<char>(new char[5]);
    int writeLength = 0;
    while ((writeLength = await streamReader.ReadBlockAsync(buffer)) > 0)
    {
        if (writeLength < buffer.Length)
        {
            buffer = buffer[..writeLength];
        }
        Console.Write(buffer.ToString());
    }
    Console.WriteLine();
    Console.WriteLine("END");
}